package com.my.screens;

import java.util.ArrayList;
import java.util.HashMap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
import com.my.simulation.Card;
import com.my.simulation.Simulation;
import com.my.drinkinggame.Main;
import com.my.drinkinggame.Renderer;
import com.my.drinkinggame.StaticFonts;
import com.my.drinkinggame.StaticSprites;

public class MyDrinkingGame extends Screen{
	public Simulation sim;
	private Main main;
	public Vector3 translationVector;
	public Renderer render;
	public Card active, next;
	public float xPos, yPos, orgXPos, orgYPos;
	public boolean sem, sweepCard, inverseSweep, noSweep, okToMove, changeCard;
	public float sweepTimer, inverseTimer;
	public boolean startedGame;

	public MyDrinkingGame(Main m, Renderer r){
		main = m;
		sim = new Simulation(m);
		render = r;
		translationVector = new Vector3();
		xPos = 0;
		yPos = 0;
		orgXPos = 0;
		orgYPos = 0;
		sweepCard = false;
		inverseSweep = false;
		noSweep = false;
		okToMove = false;
		sem = true;
		changeCard = false;
		startedGame = false;
		sweepTimer = 0;
		inverseTimer = 0;
	}

	public void getNewCard(){
		if(next != null){
			active = next;
			active.getCard().setPosition(0, 0);
		}
		else if(sim.cardList.size() > 0){
			int index = MathUtils.random(sim.cardList.size()-1);
			active = sim.cardList.get(index);
			active.getCard().setPosition(0, 0);
			sim.cardList.remove(index);
		}
		System.out.println("Number of cards left: " + sim.cardList.size());
		if(sim.cardList.size() >= 1){
			int index2 = MathUtils.random(sim.cardList.size()-1);
			next = sim.cardList.get(index2);
			active.getCard().setPosition(0, 0);
			sim.cardList.remove(index2);
		}
		else{
			next = null;
		}
		changeCard = false;
	}

	@Override
	public boolean keyDown(int arg0) {
		if (Input.Keys.BACK == arg0) {
			main.setScreen(Main.SCREEN_MAINMENU);
		}
		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int arg2, int arg3) {
		translationVector.set(x,y,0);
		render.cam.unproject(translationVector);
		orgXPos = translationVector.x;
		orgYPos = translationVector.y;
		okToMove = true;
		return false;
	}

	@Override
	public boolean touchDragged(int x, int y, int arg2) {
		translationVector.set(x,y,0);
		render.cam.unproject(translationVector);
		xPos = translationVector.x;
		yPos = translationVector.y;
		if(xPos < orgXPos && okToMove){
			active.getCard().setPosition(xPos-orgXPos, 0);
			if(xPos < orgXPos-Gdx.graphics.getWidth()/2){
				if(sem){
					sweepCard = true;
					changeCard = true;
					sem = false;
				}
			}
		}
		else{
			noSweep = true;
		}
		return false;
	}

	public boolean touchMoved(int arg0, int arg1) {
		return false;
	}

	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		sem = true;
		if(xPos > orgXPos-Gdx.graphics.getWidth()/2 && !noSweep){
			inverseSweep = true;
		}
		noSweep = false;
		okToMove = false;
		return false;
	}

	@Override
	public void update() {
		sweepTimer += Gdx.graphics.getDeltaTime()*1000;
		inverseTimer += Gdx.graphics.getDeltaTime()*1000;
		int step = 40;
		//sweep animation
		if(sweepTimer <= step*1 && sweepCard){
			active.getCard().setPosition(xPos-orgXPos-40, 0);
		}
		else if(sweepTimer <= step*2 && sweepCard){
			active.getCard().setPosition(xPos-orgXPos-80, 0);
		}
		else if(sweepTimer <= step*3 && sweepCard){
			active.getCard().setPosition(xPos-orgXPos-120, 0);
		}
		else if(sweepTimer <= step*4 && sweepCard){
			active.getCard().setPosition(xPos-orgXPos-160, 0);
		}
		else if(sweepTimer <= step*5 && sweepCard){
			active.getCard().setPosition(xPos-orgXPos-200, 0);
		}
		else if(sweepTimer <= step*6 && sweepCard){
			active.getCard().setPosition(xPos-orgXPos-240, 0);
		}
		else if(sweepTimer <= step*7 && sweepCard){
			active.getCard().setPosition(xPos-orgXPos-280, 0);
		}
		else if(sweepTimer <= step*8 && sweepCard){
			active.getCard().setPosition(xPos-orgXPos-320, 0);
		}
		else if(sweepTimer <= step*9 && sweepCard){
			active.getCard().setPosition(xPos-orgXPos-360, 0);
		}
		else if(sweepTimer <= step*10 && sweepCard){
			active.getCard().setPosition(xPos-orgXPos-400, 0);
		}
		else if(sweepTimer <= step*11 && sweepCard){
			active.getCard().setPosition(-720, 0);
			okToMove = false;

		}
		else{
			sweepCard = false;
			sweepTimer = 0;
			if(active.getCard().getBoundingRectangle().getX() <= -720 && changeCard){
				getNewCard();
			}
		}

		//inverse sweep animation
		if(inverseTimer <= step*1 && inverseSweep){
			active.getCard().setPosition(xPos-orgXPos+40, 0);
		}
		else if(inverseTimer <= step*2 && inverseSweep){
			active.getCard().setPosition(xPos-orgXPos+80, 0);
		}
		else if(inverseTimer <= step*3 && inverseSweep){
			active.getCard().setPosition(xPos-orgXPos+120, 0);
		}
		else if(inverseTimer <= step*4 && inverseSweep){
			active.getCard().setPosition(xPos-orgXPos+160, 0);
		}
		else if(inverseTimer <= step*5 && inverseSweep){
			active.getCard().setPosition(xPos-orgXPos+200, 0);
		}
		else if(inverseTimer <= step*6 && inverseSweep){
			active.getCard().setPosition(xPos-orgXPos+240, 0);
		}
		else if(inverseTimer <= step*7 && inverseSweep){
			active.getCard().setPosition(0, 0);
			okToMove = false;
		}
		else{
			inverseSweep = false;

			inverseTimer = 0;
		}

	}

	@Override
	public void render() {
		Renderer.getInstance().renderGame(this);

	}

	public void renderGame(SpriteBatch sb){
		if(next != null){
			StaticSprites.cardBGSprite2.draw(sb);
			StaticFonts.comic2_48.setScale(2f, 2f);
			StaticFonts.comic2_48.draw(sb, next.getHeader(),
					next.getCard().getWidth()/2-
					StaticFonts.comic2_48.getBounds(next.getHeader()).width/2, 
					next.getCard().getHeight()/1.05f);
		}
		else{
			StaticSprites.endCardBGSprite.draw(sb);
		}
		active.getCard().draw(sb);
		StaticFonts.comic_48.setScale(2f, 2f);
		StaticFonts.comic_48.draw(sb, active.getHeader(), active.getCard().getX()+
				active.getCard().getWidth()/2-
				StaticFonts.comic_48.getBounds(active.getHeader()).width/2, 
				active.getCard().getY()+active.getCard().getHeight()/1.05f);
		if(StaticFonts.comic_48.getBounds(active.getText()).width > active.getCard().getWidth()/2f){
			String[] listStrings = active.getText().split(" ");
			int index = 0;
			int row = 1;
			String temp = "";
			while(index < listStrings.length){
				temp += (listStrings[index] + " ");
				if(StaticFonts.comic_48.getBounds(temp).width > active.getCard().getWidth()/2f){
					StaticFonts.comic_48.draw(sb, temp, active.getCard().getX()+
							active.getCard().getWidth()/2-StaticFonts.comic_48.getBounds(temp).width/2, 
							active.getCard().getY()+active.getCard().getHeight()/1.2f-
							StaticFonts.comic_48.getBounds(temp).height*row);
					row++;			
					temp = "";
				}
				index++;
			}
			StaticFonts.comic_48.draw(sb, temp, active.getCard().getX()+
					active.getCard().getWidth()/2-StaticFonts.comic_48.getBounds(temp).width/2, 
					active.getCard().getY()+active.getCard().getHeight()/1.2f-
					StaticFonts.comic_48.getBounds(temp).height*row);

		}
		else{
			StaticFonts.comic_48.draw(sb, active.getText(), active.getCard().getX()+
					active.getCard().getWidth()/2-
					StaticFonts.comic_48.getBounds(active.getText()).width/2, 
					active.getCard().getY()+active.getCard().getHeight()/1.2f);
		}
	}

	@Override
	public void enter() {
		Gdx.input.setCatchBackKey(true);
		Gdx.input.setInputProcessor(this);
		if(!startedGame){
			sim.reset();
			sim.initiateCards();
			getNewCard();
		}
		startedGame = true;
	}

	public boolean mouseMoved(int screenX, int screenY) {

		return false;
	}

}
